<?php 
namespace app\Admin\controller;
use think\Controller;
use think\Db;
use app\Admin\model\Cate;
use app\Admin\model\Good;
use think\Request;
use think\File;

class Goods extends Base
{
	public function goods_list(){
		$intro_type = input('get.intro_type');
		$is_sale = input('get.is_sale');
		$brand_id = input('get.brand_id');
		$cat_id = input('get.cat_id');
		$keywords = input('get.keywords');
		$where = [];
		if(!empty($intro_type)){
			if($intro_type == 'is_best'){
				$where['is_best'] = 1;
			}else if($intro_type == 'is_new'){
				$where['is_new'] = 1;
			}else if($intro_type == 'is_hot'){
				$where['is_hot'] = 1;
			}
		}
		if(!empty($is_sale)){
			$where['is_sale'] = $is_sale;
		}
		if(!empty($brand_id)){
			$where['brand_id'] = $brand_id;
		}
		if(!empty($cat_id)){
			$where['cat_id'] = $cat_id;
		}
		if(!empty($keywords)){
			$where['goods_name'] =  ['like',"%$keywords%"];
		}
		$cate = new Cate;
		$cat = $cate->getPathList("cat_id");
		$brand = Db('brand')->select();
		$result = Db('goods')->where($where)->where('is_delete = 0')->order('goods_id desc')->paginate(5,false,['query'=>Request::instance()->param()]); 
		$this->assign('cat',$cat);
		$this->assign('data',$result);
		$this->assign('brand',$brand);
		return $this->fetch();
	}

	public function goods_add(){
		if(request()->isPost()){
			$good = new Good();
			$param['cat_id'] = input('post.cat_id');
			$goods_sn = input('post.goods_sn');
			if(empty($goods_sn)){
				$param['goods_sn'] = date('ymdhis').substr(microtime(),2,4);
			}else{
				$param['goods_sn'] = input('post.goods_sn');
			}
			$param['goods_name'] = input('post.goods_name');
			$param['brand_id'] = input('post.brand_id');
			$param['goods_price'] = input('post.goods_price');
			$param['is_promote'] = input('post.is_promote');
			$param['promote_price'] = input('post.promote_price');
			$param['seller_note'] = input('post.seller_note');
			$param['is_sale'] = input('post.is_sale');
			$param['goods_desc'] = input('post.goods_desc');
			$param['is_best'] = input('post.is_best');
			$param['is_new'] = input('post.is_new');
			$param['is_hot'] = input('post.is_hot');
			$param['is_shipping'] = input('post.is_shipping');
			$param['is_alone_sale'] = input('post.is_alone_sale');
			$param['type_id'] = input('post.type_id');
			$param['goods_number'] = input('post.goods_number');
			$param['goods_weight'] = input('post.goods_weight');
			$param['goods_brief'] = input('post.goods_brief');
			$param['add_time'] = time();			
			$param['img_url'] = $this->upload();		
			$result = $good->insert($param);
			$arr['goods_id'] = Db::name('goods')->getLastInsID();
			$data['attr_id'] = input('post.attr_id/a');
			$data['attr_value'] = input('post.attr_value/a');	
			for ($i=0; $i < count($data['attr_id']); $i++) { 
				$arr['attr_id'] = $data['attr_id'][$i];
				$arr['attr_value'] = $data['attr_value'][$i];
				$re = Db('goods_attr')->insert($arr);
			}
			$this->redirect('Goods/goods_list');	
		}else{
			$cate = new Cate;
			$cat = $cate->getPathList("cat_id");
			$type = Db('goods_type')->select();
			$brand = Db('brand')->select();
			$this->assign('cat',$cat);
			$this->assign('type',$type);
			$this->assign('brand',$brand);
			return $this->fetch();	
		}			
	}

	public function get_attr(){
		$type_id = input('get.id');
		$data = Db('attribute')->where('type_id',$type_id)->select();
		echo json_encode($data);
	}

	public function upload()
	{    
		$file = request()->file("img_url");
	    if($file){
	        $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
	        if($info){
	             $a=$info->getSaveName();
	             $imgp= str_replace("\\","/",$a);
	             $imgpath='/uploads/'.$imgp;
	       
	            return $imgpath;
	          
	        }else{   
	            return $file->getError();
	        }
	    }
	}

	//回收站
	public function trash(){
		$keywords = input('get.keywords');
		$where = [];
		if(!empty($keywords)){
			$where['goods_name'] =  ['like',"%$keywords%"];
		}
		$data = Db('goods')->where('is_delete',1)->where($where)->paginate(5,false,['query'=>Request::instance()->param()]);
		$this->assign('data',$data);
		return $this->fetch();
	}

	public function goods_recycle(){
		$goods_id = input('get.id');
		$result = Db('goods')->where('goods_id',$goods_id)->update(['is_delete'=>'1']);
		if($result){
			$arr = [
				'code'=>1,
				'msg'=>'已放入回收站'
			];
		}else{
			$arr = [
				'code'=>0,
				'msg'=>'放入回收站失败'
			];
		}
		echo json_encode($arr);
	}

	public function goods_restore(){
		$goods_id = input('get.id');
		$result = Db('goods')->where('goods_id',$goods_id)->update(['is_delete'=>'0']);
		if($result){
			$arr = [
				'code'=>1,
				'msg'=>'已还原'
			];
		}else{
			$arr = [
				'code'=>0,
				'msg'=>'还原失败'
			];
		}
		echo json_encode($arr);
	}

	public function goods_del(){
		$id = input('get.id');
		$result = Db('goods')->where('goods_id',$id)->delete();
		if($result){
			$arr = [
				'code'=>1,
				'msg'=>'删除成功'
			];
		}else{
			$arr = [
				'code'=>0,
				'msg'=>'删除失败'
			];
		}
		echo json_encode($arr);
	}



}//结尾